import axios from 'axios';
// import { Message } from 'element-ui'
import { Msg } from '@/components/msg'
import store from '@/vuex/store'
import router from '@/router'

/**
 * api接口基本路径
 */
export const API_BASE_URL = process.env.NODE_ENV === 'production' ? '/supervise'
    : 'https://jctest.dafyjk.net/supervise' // 测试版api地址
// : 'https://10.20.11.70/supervise' // 测试版api地址
// : 'http://10.30.3.13:8080/supervise' // 开发版api地址
// : 'http://10.30.3.14:8080/supervise' // 开发版api地址

// // http request 拦截器
// axios.interceptors.request.use(
//     config => {
//         return config
//     }, function (error) {
//         return Promise.reject(error)
//     }
// )
// // http response 拦截器
// axios.interceptors.response.use(res => {
//     // token 已过期，重定向到登录页面
//     if (res.data.code == 401) {
//         store.dispatch('loginOut')
//         // router.replace({
//         //     path: '/login',
//         //     query: { redirect: router.currentRoute.fullPath }
//         // })
//         router.replace('/login')
//     }
//     return res
// }, function (error) {
//     return Promise.reject(error)
// })

const axiosCreate = (type, path, data) => {
    let _token = store.getters.userInfo.token
    let instance = axios.create({
        headers: {
            'Authorization': _token
        }
    })
    instance.defaults.withCredentials = true

    if (type === 'get') {
        return instance({
            method: type,
            url: API_BASE_URL + path,
            params: data
        })
    } else if (type === 'post') {
        return instance({
            method: type,
            url: API_BASE_URL + path,
            data: data
        })
    }
}
const http = (type, path, data) => {
    return new Promise((resolve, reject) => {
        axiosCreate(type, path, data).then(res => {
            let _data = res.data;
            let _result = _data.data || {};
            // 请求接口成功
            if (_data.code == 10000) {
                resolve(_result, _data)
            }
            // 账号已过期
            else if (_data.code == 30001) {
                store.dispatch('clearUserInfo', () => {
                    router.replace('/login')
                })
                // router.replace({
                //     path: '/login',
                //     query: { redirect: router.currentRoute.fullPath }
                // })

                Msg.error(res.data.msg)
                reject(res)
            }
            else {
                if (_data.code != '30005') {
                    Msg.error(res.data.msg)
                }
                reject(res)
            }
        }).catch(res => {
            Msg.error('发送的响应无效')
            reject(res)
        });

    })
}

/**
 * 登录
 * @param {Object} data
 */
export const login = (data) => http('post', '/login', data)
/**
 * 退出
 */
export const logout = () => http('get', '/logout')
/**
 * 获取用户资源
 */
export const userMenus = () => http('get', '/userMenus')
/**
 * 查询字典表类型
 * @param {Object} data
 */
export const queryDict = (data) => http('get', '/dict/queryDict', data)
/**
 * 查询字典表所有信息
 */
export const queryAllDict = () => http('get', '/dict/queryAllDict')
/**
 * 所属营业部
 */
export const queryDepartment = (data) => http('get', '/salesDept/departments', data)
/**
 * 根据角色id查询用户
 */
export const queryRoleUserById = (data) => http('get', '/role/queryRoleUserById', data)
/**
 * 下载
 * @param {Object} data
 */
export const download = (data) => http('get', '/downLoad2', data)
/**
 * 修改密码
 */
export const updateUserPassword = (data) => http('post', '/user/upUserPassword', data)
/**
 * 根据条件查询角色
 */
export const queryCirculation = (data) => http('post', '/circulation/queryCirculation', data)
/**
 * 根据用户id获取其所有未分配的count
 */
export const countByUserId = () => http('get', '/caseInfo/countByUserId')
/**
 * 根据当前用户ID查询上级员工
 * @param {Object} data
 */
export const findSuperiorUser = (data) => http('get', '/user/findSuperiorUser', data)
/**
 * 查询部门区域
 * @param {Object} data（grade：等级 1 分部，2 大区， 3 区域）
 */
export const getArea = (data) => http('post', '/address/getArea', data)
/**
 * 查询省份
 */
export const getProvince = (data) => http('post', '/address/getProvince', data)
/**
 * 查询城市
 */
export const getCity = (data) => http('post', '/address/getCity', data)


/*** 线索管理 ***/
/**
* 线索列表
* @param {Object} data
*/
export const queryClue = (data) => http('post', '/clue/queryClue', data)
/**
* 查看线索详情
* @param {Object} data
*/
export const queryClueInfoById = (data) => http('get', '/clue/queryClueInfoById', data)
/**
* 作废线索
* @param {Object} data
*/
export const abolishClue = (data) => http('post', '/clue/abolishClue', data)
/**
* 提案申请
* @param {Object} data
*/
export const switchClue = (data) => http('post', '/clue/switchClue', data)
/**
* 取消提案申请
* @param {Object} data
*/
export const cancelClue = (data) => http('post', '/clue/cancelClue', data)
/**
* 添加线索
* @param {Object} data
*/
export const addClue = (data) => http('post', '/clue/addClue', data)
/**
* 编辑线索
* @param {Object} data
*/
export const editClue = (data) => http('post', '/clue/eidClue', data)
/**
* 编辑线索
* @param {Object} data
*/
export const eidtClue = (data) => http('post', '/clue/eidtClue', data)
/**
* 查询所有作废线索
* @param {Object} data
*/
export const queryAbolishClue = (data) => http('post', '/clue/queryAbolishClue', data)
/**
* 线索详细----删除证据
* @param {Object} data
*/
export const deleteClueEvidence = (data) => http('post', '/clue/delClueEvidence', data)
/**
* 线索详细的添加风险
* @param {Object} data
*/
export const addCaseRisk = (data) => http('post', '/caseRisk/addCaseRisk', data)
/**
* 线索详细的风险删除
* @param {Object} data
*/
export const delCaseRisk = (data) => http('get', '/caseRisk/delCaseRisk', data)
/**
* 线索详细和案件详细中的风险列表
* @param {Object} data
*/
export const caseRiskList = (data) => http('post', '/caseRisk/pageList', data)


/*** 案件管理 ***/
/**
* 添加案件
* @param {Object} data
*/
export const addCase = (data) => http('post', '/caseInfo/addCaseInfoVo', data)
/**
* 修改案件
* @param {Object} data
*/
export const editCaseInfoVo = (data) => http('post', '/caseInfo/editCaseInfoVo', data)
/**
* 查询案件列表
* @param {Object} data
*/
export const queryCase = (data) => http('post', '/caseInfo/selectPageList', data)
/**
* 分配用户列表
* @param {Object} data
*/
export const findSubordinateUser = (data) => http('get', '/user/findSubordinateUser', data)
/**
* 查询我的案件（所有案件，暂停案件，转案案件，结案案件）
* @param {Object} data
*/
export const queryMyCasePageList = (data) => http('post', '/caseInfo/queryMyCasePageList', data)
/**
* 案件详细
* @param {Object} data
*/
export const caseDetail = (data) => http('get', '/caseInfo/detailCaseInfoVo', data)
/**
* 案件详细----轨迹列表
* @param {Object} data
*/
export const caseTrackList = (data) => http('post', '/caseTrack/pageList', data)
/**
* 案件详细----报告列表
* @param {Object} data
*/
export const caseReportList = (data) => http('post', '/caseReport/pageList', data)
/**
* 案件详细----删除报告
* @param {Object} data
*/
export const deleteCaseReport = (data) => http('get', '/caseReport/deleteCaseReport', data)
/**
* 案件详细----添加报告
* @param {Object} data
*/
export const addCaseReport = (data) => http('post', '/caseReport/addCaseReport', data)
/**
* 案件详细----调查列表
* @param {Object} data
*/
export const caseRecordList = (data) => http('post', '/caseRecord/pageList', data)
/**
* 案件详细----添加调查记录
* @param {Object} data
*/
export const addCaseRecord = (data) => http('post', '/caseRecord/addCaseRecord', data)
/**
* 线索详细----证据列表
* @param {Object} data
*/
export const clueEvidenceList = (data) => http('post', '/clue/pageList', data)
/**
* 案件详细----证据列表
* @param {Object} data
*/
export const caseEvidenceList = (data) => http('post', '/caseEvidence/pageList', data)
/**
* 案件详细----添加证据
* @param {Object} data
*/
export const addCaseEvidence = (data) => http('post', '/caseEvidence/addCaseEvidence', data)
/**
* 线索详细----添加证据
* @param {Object} data
*/
export const addClueEvidence = (data) => http('post', '/clue/addClueEvidence', data)
/**
* 案件详细----删除证据
* @param {Object} data
*/
export const deleteCaseEvidence = (data) => http('get', '/caseEvidence/deleteCaseEvidence', data)
/**
* 暂停案件
* @param {Object} data
*/
export const suspendCase = (data) => http('post', '/caseInfo/suspendCase', data)
/**
* 取消案件审批流程申请
* @param {Object} data
*/
export const cancelCaseAuditApply = (data) => http('post', '/caseInfo/cancelCaseAuditApply', data)
/**
* 恢复已暂停的案件
* @param {Object} data
*/
export const recoverCase = (data) => http('post', '/caseInfo/recoverCase', data)
/**
* 退回案件
* @param {Object} data
*/
export const sendBackCase = (data) => http('post', '/caseInfo/sendBackCase', data)
/**
* 收回案件
* @param {Object} data
*/
export const takeBackCase = (data) => http('post', '/caseInfo/takeBackCase', data)
/**
* 转案
* @param {Object} data
*/
export const switchCase = (data) => http('post', '/caseInfo/switchCase', data)
/**
* 结案
* @param {Object} data
*/
export const closeCase = (data) => http('post', '/caseInfo/closeCase', data)
/**
* 分配所有未分配的案件
* @param {Object} data
*/
export const manualAllotAllCase = (data) => http('post', '/caseInfo/manualAllotAllCase', data)
/**
* 分配选中案件
* @param {Object} data
*/
export const allotSelectedCase = (data) => http('post', '/caseInfo/allotSelectedCase', data)


/*** 流程审批 ***/
/**
 * 查询审批列表
 * @param {Object} data
 */
export const queryCaseExamine = (data) => http('post', '/caseExamine/queryCaseExamine', data)
/**
 * 查询提案审批列表
 * @param {Object} data
 */
export const queryClueExamine = (data) => http('post', '/caseExamine/queryClue', data)
/**
 * 修改审批
 * @param {Object} data
 */
export const updateCaseExamine = (data) => http('post', '/caseExamine/updateCaseExamine', data)
/**
 * 查询审批详情
 * @param {Object} data
 */
export const queryCaseDetailed = (data) => http('post', '/caseExamine/queryCaseDetailed', data)


/*** 系统管理 ***/

// 用户管理
/**
 * 查询组织架构
 *
 */
export const queryDept = () => http('get', '/user/queryDept')

/**
 * 编辑组织架构
 * @param {Object} obj
*  @param {Number} obj.type 0 表示添加  1 表示修改
 */
export const editDept = (obj) => {
    let _apiUrl = '/user/addDept'
    if (obj.type == 1) {
        _apiUrl = '/user/editDept'
    }
    return http('post', _apiUrl, obj.data)
}
/**
 * 删除组织架构
 * @param {Object} data
 */
export const deleteDept = (data) => http('post', '/user/deleteDept', data)

/**
 * 编辑用户
 * @param {Object} obj
* @param {Number} obj.type 0 表示添加  1 表示修改
 */
export const editUser = (obj) => {
    let _apiUrl = '/user/addUser'
    if (obj.type == 1) {
        _apiUrl = '/user/updateUser'
    }
    return http('post', _apiUrl, obj.data)
}
/**
 * 删除用户
 * @param {Object} data
 */
export const deleteUser = (data) => http('post', '/user/deleteUser', data)
/**
 * 查询单个用户
 * @param {Object} data
 */
export const getUserDetail = (data) => http('get', '/user/queryUserById', data)
/**
 * 查询用户列表
 * @param {Object} data
 */
export const queryUser = (data) => http('post', '/user/queryUser', data)


// 角色管理
/**
 * 编辑角色
 * @param {Object} obj
* @param {Number} obj.type 0 表示添加  1 表示修改
 */
export const editRole = (obj) => {
    let _apiUrl = '/role/addRole'
    if (obj.type == 1) {
        _apiUrl = '/role/editRole'
    }
    return http('post', _apiUrl, obj.data)
}
/**
 * 删除角色
 * @param {Object} data
 */
export const delRole = (data) => http('post', '/role/delRole', data)
/**
 * 修改角色权限
 * @param {Object} data
 */
export const editDiction = (data) => http('post', '/role/editDiction', data)
/**
 * 查询所有子类角色
 * @param {Object} data
 */
export const queryRole = (data) => http('get', '/role/queryRole', data)
/**
 * 查询角色资源
 * @param {Object} data
 */
export const getRoleResource = (data) => http('get', '/role/getRoleResource', data)

// 架构管理
/*
* 编辑架构
* @param {Object} obj
* @param {Number} obj.type 0 表示添加  1 表示修改
*/
export const editOrg = (obj) => {
    let _apiUrl = '/salesDept/addSalesDept'
    if (obj.type == 1) {
        _apiUrl = '/salesDept/editSalesDept'
    }
    return http('post', _apiUrl, obj.data)
}

/**
* 删除架构
* @param {Object} data
*/
export const deleteOrg = (data) => http('post', '/salesDept/delSalesDept', data)

/**
* 获取架构列表
* @param {Object} data
*/
export const getOrgList = (data) => http('post', '/salesDept/salesDeptList', data)

/**
* 获取架构列表ByParentCode
* @param {Object} data
*/
export const queryOrgList = (data) => http('get', '/salesDept/queryByParentCode', data)

/**
* 获取单个机构
* @param {Object} data
*/
export const queryOrgDetail = (data) => http('post', '/salesDept/getSalesDept', data)

/**
* 编辑架构用户
* @param {Object} obj
* @param {type} 0 表示添加  1 表示修改
*/
export const editOrgUser = (obj) => {
    var _apiUrl = '/orgUser/addOrgUser'
    if (obj.type == 1) {
        _apiUrl = '/orgUser/editOrgUser'
    }

    return http('post', _apiUrl, obj.data)
}
/**
* 删除机构用户
* @param {Object} data
*/
export const delOrgUser = (data) => http('get', '/orgUser/delOrgUser', data)

/**
* 查询机构用户列表
* @param {Object} data
*/
export const getOrgUserList = (data) => http('post', '/orgUser/selectPageList', data)

/**
* 查询单个用户
* @param {Object} data
*/
export const getOrgUser = (data) => http('get', '/orgUser/getOrgUser', data)


// 资源管理
/**
 * 编辑资源
* @param {Object} obj
* @param {type} 0 表示添加  1 表示修改
*/
export const editResource = (obj) => {
    var _apiUrl = '/resource/saveResource'
    if (obj.type == 1) {
        _apiUrl = '/resource/editResource'
    }

    return http('post', _apiUrl, obj.data)
}
/**
 * 删除角色资源
 * @param {Object} data
 */
export const deleteResource = (data) => http('post', '/resource/deleteResource', data)
/**
 * 查询资源列表
 * @param {Object} data
 */
export const queryAllResource = (data) => http('get', '/resource/queryAllResource', data)


/****营业部管理****/
/**
 * 编辑营业部
* @param {Object} obj
* @param {type} 0 表示添加  1 表示修改
*/
export const editSalesDeptJc = (obj) => {
    var _apiUrl = '/deptJc/addSalesDeptJc'
    if (obj.type == 1) {
        _apiUrl = '/deptJc/editSalesDeptJc'
    }

    return http('post', _apiUrl, obj.data)
}
/**
 * 删除营业部
 * @param {Object} data
 */
export const delSalesDeptJc = (data) => http('get', '/deptJc/delSalesDeptJc', data)
/**
 * 查询营业部
 * @param {Object} data
 */
export const queryDeptJc = (data) => http('post', '/deptJc/queryDept', data)
/**
 * 查询营业部详情
 * @param {Object} data
 */
export const getSalesDeptJc = (data) => http('get', '/deptJc/getSalesDeptJc', data)


/****消息****/

/**
 * 消息列表
 */
export const messageList = (data) => http('post', '/caseMessage/pageList', data)
/**
 * 修改消息
 */
export const editCaseMessage = (data) => http('post', '/caseMessage/editCaseMessage', data)
/**
 * 修改消息
 */
export const updateRead = (data) => http('post', '/caseMessage/updateRead', data)
/**
 * 修改消息
 */
export const delCaseMessage = (data) => http('post', '/caseMessage/delCaseMessage', data)

